package org.example.studentmanager.service.impl;

import jakarta.transaction.Transactional;
import org.example.studentmanager.dao.ClassDao;
import org.example.studentmanager.dao.CourseDao;
import org.example.studentmanager.dao.StudentDao;
import org.example.studentmanager.entity.*;
import org.example.studentmanager.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class StudentServiceImpl implements StudentService {
    private static final Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);

    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ClassDao classDao;
    @Autowired
    private CourseDao courseDao;

    /**
     * 根据id获取学生的全部成绩
     * @param id 学生id
     * @return 学生的全部成绩信息
     */
    @Override
    public List<Score> getScores(Integer id) {
        return studentDao.getScore(id);
    }

    @Override
    // 实现查询学生选课信息的方法
    public List<CourseSelectionsStudent> getCourseSelectionsByStudentId(Integer id) {
        List<CourseSelectionsStudent> selections = studentDao.getCourseSelectionsByStudentId(id);
        logger.info("查询到的选课记录: {}", selections);
        return selections;
    }

    /**
     * 根据学生id查询相关学生信息
     * @param userId 学生id
     * @return 学生相关信息
     * */
    @Override
    public Student getStudentById(Integer userId) {
        return studentDao.getStudentById(userId);
    }

    /**
     * 学生批量选课，批处理竟然用batch这个词，AI果然高级
     * @param selections 选课记录数组
     * @return Mybatis自动的return，可能是什么修改记录条数吧
     * */
    @Override
    public int batchSelectCourses(List<CourseSelectionsStudent> selections) {
        // 为每个选课记录设置当前时间
        for (CourseSelectionsStudent selection : selections) {
            selection.setDate_time(LocalDateTime.now());
        }
        // 将学生加入相应的班级
//        for (CourseSelectionsStudent selection : selections) {
//            Course course = courseDao.queryCourseByCourseId(selection.getCourseId());
//            classDao.addStudent(classDao.findClazzByCourseName(course.getCname()).get(0).getId(), selection.getStudentId());
//        }
        return studentDao.batchInsertSelections(selections);
    }

    /**
     * 根据学生id和课程id删除对应的选课记录
     * @param studentId 学生id
     * @param courseId 课程id
     * @return 删除是否成功，成功返回ture，失败返回错误信息
     */
    @Override
    @Transactional
    public Boolean dropCourse(Integer studentId, Integer courseId) {
        // 只删除选课记录，不再涉及班级操作
        int deleteCount = studentDao.dropCourse(studentId, courseId);
        return deleteCount > 0;
    }
    @Override
    public List<CourseSelectionsStudent> getCourseSelectionsWithDetails(Integer studentId) {
        return studentDao.getCourseSelectionsWithDetails(studentId);
    }

    @Override
    public List<CourseSelectionsStudent> getNewCourseSelectionsByStudentId(Integer studentId) {
        return List.of();
    }
}

